package com.yc.sole.system.controller;

import com.yc.sole.common.domain.UserInfo;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.web.PreAuth;
import com.yc.sole.system.biz.AuthBiz;
import com.yc.sole.system.request.LoginReq;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 平台登录
 *
 * @author yizuomin
 * @date 2023/3/10 10:00
 **/
@RestController
@RequiredArgsConstructor
public class AuthController {

    private final AuthBiz authBiz;

    /**
     * 验证码
     *
     * @param sessionId
     * @return
     */
    @GetMapping("/api/auth/captcha/{sessionId}")
    public Result<String> captcha(@PathVariable String sessionId) {
        return Result.data(authBiz.captcha(sessionId));
    }

    /**
     * 登录
     *
     * @param loginReq
     * @param request
     * @return
     */
    @PostMapping("/api/auth/login")
    public Result<String> login(@Validated @RequestBody LoginReq loginReq, HttpServletRequest request) {
        return Result.data(authBiz.webLogin(loginReq, request));
    }

    /**
     * 登出
     *
     * @return
     */
    @PreAuth(value = "logout", description = "登出", check = false)
    @PostMapping("/api/auth/logout")
    public Result<Void> logout(HttpServletRequest request) {
        authBiz.logout(request);
        return Result.success();
    }

    /**
     * 用户信息
     *
     * @return
     */
    @GetMapping("/api/auth/info")
    public Result<UserInfo> userInfo(HttpServletRequest request) {
        return Result.data(authBiz.userInfo(request));
    }
}
